﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;
using SIG.Resources.Admin;
using SIG.Model.Identity;

namespace SIG.Service
{
    public class MenuCategoryService : EntityService<MenuCategory>, IMenuCategoryService
    {

        private readonly IMenuCategoryRepository _categoryRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public MenuCategoryService(IMenuCategoryRepository categoryRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(categoryRepository, cacheService, loggingService)
        {
            _categoryRepository = categoryRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }


        //public MenuCategory GetCategoryBySeoName(string seoName)
        //{

        //    var key = $"{EntityNames.MenuCategory}_bySeoName_{seoName}";
        //    if (!SettingsManager.Article.EnableCaching)
        //    {
        //        return _categoryRepository.GetFirstOrDefault(p => p.Active & p.SeoName.Equals(seoName, System.StringComparison.InvariantCultureIgnoreCase));
        //    }

        //    if (_cacheService.IsSet(key))
        //    {
        //        return (MenuCategory)_cacheService.Get(key);

        //    }

        //    var category = _categoryRepository.GetFirstOrDefault(p => p.Active & p.SeoName.Equals(seoName, System.StringComparison.InvariantCultureIgnoreCase)); ;
        //    _cacheService.Set(key, category, SettingsManager.Article.CacheDuration);


        //    return category;
        //}

        public List<MenuCategory> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.MenuCategory}_ListCount_{keyword}";

            var totalIQuery = _categoryRepository.GetAllElements().Where(g => g.Active);
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));

            if (SettingsManager.Menu.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Menu.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.MenuCategory}_List_{pageIndex}_{pageSize}_{keyword}";

            List<MenuCategory> categories;
            Expression<Func<MenuCategory, bool>> filter = g => g.Active;
            Expression<Func<MenuCategory, bool>> filterByKeyword = g => g.Title.Contains(keyword);


            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);


            if (SettingsManager.Menu.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<MenuCategory>)_cacheService.Get(key);

                }
                else
                {
                    categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Menu.CacheDuration);
                }
            }
            else
            {
                categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
            }

            return categories;

        }

    }
}
